/*
 * ds18b20.c
 *
 *  Created on: 21/01/2015
 *      Author: santiago
 */

#include "ds18b20.h"

/**
 * FUNCIONES ROM PARA DS18B20
 */



error_t ds18b20_getTemp(owDevice_t * sensor, int16_t *temperatura)
{
	error_t retVal;
	ds18b20SP_u resultado;
	owReset();
	owSendMatchRom(sensor);
	ds18b20_Convert();
	owWaitPin(1000000);
	owReset();
	owSendMatchRom(sensor);
	owSendCmd(0xBE);
	retVal = owReadBytes(8,resultado.raw.bytes);
	*temperatura = resultado.spad.temperatura;
	return retVal;
}


error_t ds18b20_getScratchPad(owDevice_t * sensor, ds18b20rawSPad_t *scratch)
{
	error_t retVal;
	owReset();
	owSendMatchRom(sensor);
	ds18b20_Convert();
	owWaitPin(1000000);
	owReset();
	owSendMatchRom(sensor);
	owSendCmd(0xBE);
	retVal = owReadBytes(8,scratch->bytes);
	return retVal;
}

error_t ds18b20_setCfg(owDevice_t *sensor, uint8_t *datos)
{
	error_t retVal = ret_Ok;
	owReset();
	owSendMatchRom(sensor);
	owSendCmd(0x4e);
	owSendCmd(datos[0]);
	owSendCmd(datos[1]);
	owSendCmd(datos[2]);
	return retVal;

}


error_t ds18b20_Convert(void)
{
	return owSendCmd(0x44);
}
